package Test;

import DB.SemesterDB;
import Model.Semester;

import java.util.ArrayList;

import static org.junit.Assert.fail;

public class SemesterDBTest {
    //OK
    @org.junit.Test
    public void testInsertSemester() throws Exception {
        SemesterDB semesterDB = new SemesterDB();
        int rc = semesterDB.insertSemester(new Semester(4, 4, "2013-09-02", "2013-12-20"));

        System.out.println(rc);

        if (rc == 1) {
            System.out.println("Inserting semester successful");
        }
        else {
            fail("Failed inserting semester");
        }
    }
    //OK
    @org.junit.Test
    public void testFindSemester() throws Exception {

        SemesterDB semesterDB = new SemesterDB();
        int find = 2;

        Semester semester = semesterDB.findSemester(find);

        if (semester != null) {
            System.out.println(semester.getId());
            System.out.println(semester.getSemesterNo());
            System.out.println(semester.getStartDate());
            System.out.println(semester.getEndDate());
        }
        else {
            fail("Failed finding semester - semester is null");
        }
    }
    //OK
    @org.junit.Test
    public void testGetAllSemesters() throws Exception {
        SemesterDB SemesterDB = new SemesterDB();
        ArrayList<Semester> semesterObj = SemesterDB.getAllSemesters(false);

        if (semesterObj != null) {
            for (Semester s : semesterObj) {
                System.out.println(s.getSemesterNo());
                System.out.println(s.getSemesterNo());
                System.out.println(s.getStartDate());
                System.out.println(s.getEndDate());
            }
        }
        else {
            fail("No Semesters found");
        }
    }
    //OK
    @org.junit.Test
    public void testUpdateSemester() throws Exception {
        SemesterDB semesterDB = new SemesterDB();
        int updateValue = 5;

        Semester semester = new Semester(2, updateValue, "2013-09-02", "2013-12-20");

        semesterDB.updateSemester(semester);

        int semesterNo = semesterDB.findSemester(2).getSemesterNo();

        if (semesterNo == updateValue) {
            System.out.println("Update successful");
        }
        else {
            fail("Update failed");
        }

    }
    //OK!
    @org.junit.Test
    public void testDeleteSemester() throws Exception {
        SemesterDB semesterDB = new SemesterDB();
        Semester semester = new Semester(1000, 2, "2013-09-02", "2013-12-20");
        semesterDB.insertSemester(semester);

        if (semesterDB.findSemester(1000) != null) {

            semesterDB.deleteSemester(1000);

            if (semesterDB.findSemester(1000) == null) {
                System.out.println("Delete successful");
            }
            else {
                fail("Delete not successful");
            }
        }
        else {
            fail("Inserting for deletion failed");
        }
    }
}
